package com.apps.ftapp.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.IdRes;

import com.apps.ftapp.R;
import com.apps.ftapp.adapter.MyErrorSubjectRecAdapter;
import com.apps.ftapp.model.RecSubjectModel;
import com.apps.ftapp.util.ActivitySkipUtil;
import com.apps.ftapp.util.Async;
import com.apps.ftapp.util.Constant;
import com.apps.ftapp.util.L;
import com.apps.ftapp.util.StatusBarUtils;
import com.apps.ftapp.util.Tools;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cz.msebera.android.httpclient.Header;
import info.hoang8f.android.segmented.SegmentedGroup;

/**
 * 我的推荐错题
 */
public class MyErrorSubjectRecActivity extends Activity implements View.OnClickListener {


    @BindView(R.id.listView)
    ListView listView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.wodecuoti)
    RadioButton wodecuoti;
    @BindView(R.id.tuijiancuoti)
    RadioButton tuijiancuoti;
    @BindView(R.id.segmented2)
    SegmentedGroup segmented2;
    @BindView(R.id.backBtn)
    ImageView backBtn;


    private Gson gson = new Gson();

    private List<RecSubjectModel.DataBean> dataList = new ArrayList();
    private MyErrorSubjectRecAdapter myErrorSubjectRecAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_error_subject_rec);
        ButterKnife.bind(this);

        initVIew();
        initEvent();
        StatusBarUtils.StatusBarLightMode(this);
        loadData();

    }

    /**
     *
     */

    private void loadData() {
        RequestParams params = new RequestParams();
        params.put("token", Constant.token);
        params.put("userId", Constant.uid);
        Async.post("top/quiz", params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                if (statusCode == 200) {
                    L.i("main", new String(responseBody));
                    RecSubjectModel jsonBean = gson.fromJson(new String(responseBody), RecSubjectModel.class);
                    if (Tools.isIntNull(jsonBean.getSuccess()) == 1) {
                        dataList.addAll(jsonBean.getData());
                        myErrorSubjectRecAdapter.addRes(jsonBean.getData());

                    }
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

            }
        });
    }

    private void initVIew() {


        segmented2.setTintColor(getResources().getColor(R.color.index_zhenti_btn_color));

        tuijiancuoti.setChecked(true);
        backBtn.setOnClickListener(this);

        myErrorSubjectRecAdapter = new MyErrorSubjectRecAdapter(getApplicationContext(), dataList);
        listView.setAdapter(myErrorSubjectRecAdapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HashMap map = new HashMap();
                map.put("id", dataList.get(position).getQuestionId());
//
//                L.i("main",":::::"+dataList.get(position).getQuestionId());
//

//                ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectRecActivity.this, StErrorJxActivity.class, map);


                ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectRecActivity.this, MyErrorDetailActivity.class, map);
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.jiaoshizhaipin:

                break;

            case R.id.backBtn:

                finish();

                break;
        }
    }

    private void initEvent() {
        segmented2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                switch (i) {

                    case R.id.wodecuoti:

                        ActivitySkipUtil.skipAnotherActivity(MyErrorSubjectRecActivity.this, MyErrorSubjectActivity.class, true);


                        break;

                    case R.id.tuijiancuoti:


                        break;
                }
            }
        });
    }

}
